package leetcode.code0833;

import java.util.Arrays;

import leetcode.IDebug;

public class Solution implements IDebug {

	public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) {
		int k = indices.length;
		int[][] idx = new int[k][];
		for (int i = 0; i < k; i++) {
			idx[i] = new int[] { indices[i], i };
		}
		Arrays.sort(idx, (a, b) -> a[0] - b[0]);
		char[] cs = s.toCharArray();
		int last = 0, len = cs.length;
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < k; i++) {
			int[] id = idx[i];
			int start = id[0], src = id[1];
			sb.append(new String(cs, last, start - last));
			last = start;
			int size = sources[src].length();
			String ori = new String(cs, start, Math.min(size, len - start));
			if (ori.equals(sources[src])) {
				sb.append(targets[src]);
				last = start + size;
			}
		}
		sb.append(new String(cs, last, cs.length - last));
		return sb.toString();
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
